package chapter.android.aweme.ss.com.homework;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 作业2：一个抖音笔试题：统计页面所有view的个数
 * Tips：ViewGroup有两个API
 * {@link android.view.ViewGroup #getChildAt(int) #getChildCount()}
 * 用一个TextView展示出来
 */
public class Exercises2 extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(this).inflate(R.layout.activity_chatroom, null);
        setContentView(view);
        int viewCount = getViewCount(view);
        System.out.println("View count = " + viewCount);

        TextView tv = findViewById(R.id.tv_content_info);
        tv.setText("View count = " + viewCount);

    }

    public static int getViewCount(View view) {
        //todo 补全你的代码
        int viewCount = 0;
        if(view == null){
            return 0;
        }
        if(view instanceof ViewGroup){
            viewCount++;
            for(int i = 0; i < ((ViewGroup)view).getChildCount(); i++){
                View temp = ((ViewGroup)view).getChildAt(i);
                if(temp instanceof ViewGroup){
                    viewCount += getViewCount(temp);
                }
                else{
                    viewCount++;
                }
            }
        }
        return viewCount;
    }
}
